﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BL.BetterControls
{
    [ToolboxData("<{0}:_DataPagerFieldItem runat=server NextText=\"&raquo;\"PreviousText=\"&laquo;\" ButtonCount=\"10\"></{0}:_DataPagerFieldItem>")]
    public class _ZurbPagerField : DataPagerField
    {
        private string _previousText = "&laquo;";
        private string _nextText = "&raquo;";
        private int _buttonCount = 5;

        public string PreviousText
        {
            get { return this._previousText; }
            set { this._previousText = value; }
        }

        public string NextText
        {
            get { return this._nextText; }
            set { this._nextText = value; }
        }

        public int ButtonCount
        {
            get { return this._buttonCount; }
            set { this._buttonCount = value; }
        }



        protected override DataPagerField CreateField()
        {
            return new _ZurbPagerField();
        }



        private void CreatePages(Control container, IEnumerable<int> pages, int current)
        {
            foreach (int page in pages)
            {
                    HtmlGenericControl LI = new HtmlGenericControl("li");

                    LinkButton button = new LinkButton
                    {
                        Text = page.ToString(),
                        CommandName = "Page",
                        CommandArgument = page.ToString()

                    };

                    if (page == current)
                    {
                        button.Enabled = false;
                        button.CssClass = "disabled";
                        LI.Attributes["class"] = "current";
                    }                    

                    LI.Controls.Add(button);

                    container.Controls.Add(LI);

                
            }
        }

        private void CreateLink(Control container, string caption,string command, bool enabled)
        {
            HtmlGenericControl LI = new HtmlGenericControl("li");

            LI.Attributes["class"] = "arrow";

            if (!enabled)
            {
                LI.Attributes["class"] = " unavailable";
            }
            
            LinkButton button = new LinkButton
            {
                Text = caption,
                CommandName = command,
                Enabled = enabled
            };

            if (!enabled)
            {
                button.CssClass = "disabled";
            }

            LI.Controls.Add(button);

            container.Controls.Add(LI);
        }

        public override void CreateDataPagers(DataPagerFieldItem container, int startRowIndex,int maximumRows, int totalRowCount, int fieldIndex)
        {
            // The pages here are 0-based. (Page 1 = 0, Page 2 = 1, ...)
            int page = (startRowIndex / maximumRows);
            int totalPages = (totalRowCount / maximumRows);

            if(totalPages * maximumRows < totalRowCount)
            {
                totalPages += 1;
            }

            // Create a container.
            HtmlGenericControl pagerUl = new HtmlGenericControl("ul");
            pagerUl.Attributes["class"] = "pagination";
            container.Controls.Add(pagerUl);

            CreateLink(pagerUl, PreviousText, "Prev", page > 0);

            int firstPage = Math.Max(page - (this._buttonCount / 2), 0);
            int lastPage = Math.Min(firstPage + this._buttonCount, totalPages);
            int count = lastPage - firstPage;

            if (count < this._buttonCount)
            {
                firstPage = Math.Max(lastPage - this._buttonCount, 0);
                count = lastPage - firstPage;
            }

            CreatePages(pagerUl,Enumerable.Range(firstPage + 1, count), page + 1);

            CreateLink(pagerUl, NextText, "Next", (page + 1) < totalPages);
        }

        public override void HandleEvent(CommandEventArgs e)
        {
            int newStartRowIndex = -1;

            if (e.CommandName.Equals("Prev"))
            {
                if (this.DataPager.StartRowIndex >= this.DataPager.PageSize)
                {
                    newStartRowIndex = this.DataPager.StartRowIndex -
                        this.DataPager.PageSize;
                }
            }
            else if (e.CommandName.Equals("Next"))
            {
                if ((this.DataPager.StartRowIndex + this.DataPager.PageSize) <
                    this.DataPager.TotalRowCount)
                {
                    newStartRowIndex = this.DataPager.StartRowIndex +
                        this.DataPager.PageSize;
                }
            }
            else if (e.CommandName.Equals("Page"))
            {
                int page;

                if (int.TryParse(e.CommandArgument.ToString(), out page))
                {
                    int newIndex = (page - 1) * this.DataPager.PageSize;

                    if ((newIndex >= 0) ||
                        (newIndex < this.DataPager.TotalRowCount))
                    {
                        newStartRowIndex = newIndex;
                    }
                }
            }

            if (newStartRowIndex >= 0)
            {
                this.DataPager.SetPageProperties(newStartRowIndex,
                    this.DataPager.MaximumRows, true);
            }
        }



    }




}